package com.lenovo.smarttraffic.ui.activity.stage5;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.BusLineInfoBean;
import com.lenovo.smarttraffic.bean.BusStationInfoBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BusFindActivity extends BaseActivity {

    public static List<BusLineInfoBean.ROWSDETAILBean> lineList;
    public static List<BusStationInfoBean.ROWSDETAILBean> stationList = new ArrayList<>();
    public static String capacity;

    private TextView id_tv, time_tv, people_tv;
    private Button info_btn;

    private ExpandableListView listView;
    private BusFindAdapter adapter;

    private Gson gson = new Gson();

    private Handler handler = new Handler();

    @Override
    protected int getLayout() {
        return R.layout.activity_bus_find;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "公交查询");

        id_tv = findViewById(R.id.id_tv);
        time_tv = findViewById(R.id.time_tv);
        people_tv = findViewById(R.id.people_tv);
        info_btn = findViewById(R.id.info_btn);

        listView = findViewById(R.id.ex_listView);

        initData();
    }

    private void initData() {
        HttpUtils.post("get_busline_info", new HttpUtils().add("UserName", MainActivity.userName), response -> {
            BusLineInfoBean busLineInfoBean = new Gson().fromJson(response.toString(), BusLineInfoBean.class);
            lineList = busLineInfoBean.getROWS_DETAIL();

            info_btn.setOnClickListener(v -> {
                int capacity = 0;
                for (int i = 0; i < 15; i++) {
                    capacity += Utils.getRandom(56, 101);
                }
                View view = View.inflate(BusFindActivity.this, R.layout.dialog_bus_capacity, null);

                AlertDialog ad = new AlertDialog.Builder(BusFindActivity.this)
                        .setTitle("交车载客情况统计")
                        .setView(view)
                        .show();

                TextView total_tv = view.findViewById(R.id.total_tv);
                total_tv.setText(capacity + "");
                Button back_btn = view.findViewById(R.id.back_btn);
                back_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.dismiss();
                    }
                });
            });

            refresh.run();
        });
    }

    private Runnable refresh = new Runnable() {
        @Override
        public void run() {
            HttpUtils.post("get_bus_capacity", new HttpUtils().add("UserName", MainActivity.userName), response2 ->
                    HttpUtils.post("get_bus_station_info", new HttpUtils().add("UserName", MainActivity.userName), response3 -> {
                        capacity = response2.get("BusCapacity").toString();
                        people_tv.setText("当前承载能力：" + (Utils.parseInt(capacity) * 15) + "人");

                        BusStationInfoBean busStationInfoBean = gson.fromJson(response3.toString(), BusStationInfoBean.class);
                        stationList.clear();
                        stationList.add(busStationInfoBean.getROWS_DETAIL().get(0));
                        stationList.add(busStationInfoBean.getROWS_DETAIL().get(1));

                        Collections.sort(stationList, (o1, o2) -> o1.getDistance() - o2.getDistance());

                        time_tv.setText("首：" + lineList.get(0).getStart_place_start_time().substring(0, 5) + "  末：" + lineList.get(0).getStart_place_end_time().substring(0, 5));

                        if (adapter == null) {
                            adapter = new BusFindAdapter();
                            listView.setAdapter(adapter);
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                    }));

            handler.postDelayed(refresh, 3000);
        }
    };

}

class BusFindAdapter extends BaseExpandableListAdapter {


    public BusFindAdapter() {
    }

    @Override
    public int getGroupCount() {
        return 2;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 2;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_traffic_group, parent, false);
            viewHolder = new GroupViewHolder();
            viewHolder.title = convertView.findViewById(R.id.title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }

        BusStationInfoBean.ROWSDETAILBean bean = BusFindActivity.stationList.get(groupPosition);
        if (bean.getBusId() == 1) {
            viewHolder.title.setText("中医院站");
        } else {
            viewHolder.title.setText("联想大厦站");
        }

        return convertView;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_busfind_child, parent, false);
            viewHolder = new ChildViewHolder();
            viewHolder.id_tv = convertView.findViewById(R.id.id_tv);
            viewHolder.distant_tv = convertView.findViewById(R.id.distant_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }

        BusStationInfoBean.ROWSDETAILBean bean = BusFindActivity.stationList.get(childPosition);
        int distant = bean.getDistance() / 10;
        int min = distant / 333;
        viewHolder.id_tv.setText((childPosition + 1) + "号" + "(" + BusFindActivity.capacity + "人)      " + min + "分钟到达");
        viewHolder.distant_tv.setText("距离站台" + distant + "米");

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //指定位置上的子元素是否可选中
        return true;
    }

    static class GroupViewHolder {
        TextView title;
    }

    static class ChildViewHolder {
        TextView id_tv, distant_tv;
    }
}